//
//  TVCardDetailCell.swift
//  iPhone
//
//  Created by Toj on 8/10/22.
//

import UIKit

protocol TVCardDetailCellDelegate: NSObjectProtocol {
    func cardDetailCellDidTapUnlink()
    
    func cardDetailCellDidTapPreOtherCard(_ view: TVCardDetailCell)
    func cardDetailCell(_ view: TVCardDetailCell, didTapPrelinkCard activity: ActivityModel)
}

// MARK: - 懒加载(属性), 变量, 初始化init
class TVCardDetailCell: UICollectionViewCell {
    
    weak var delegate: TVCardDetailCellDelegate?
    
    var model: TVCardCellModel? { didSet {
        
        guard let card = model else {
            // 预绑卡信息界面
            if let activity = STUniversalLinksManager.shared.activityModel {
                prelinkInfoView.isHidden = false
                unlinkView.isHidden = true
                linkedView.isHidden = true
                prelinkInfoView.activity = activity
                STUniversalLinksManager.shared.activityModel = nil
                
                contentView.addSubview(prelinkInfoView)
                prelinkInfoView.snp.makeConstraints { make in
                    make.edges.equalTo(contentView)
                }
                
                DataStatistics.shared.ds_linkInformation(category: .dvbservice, action: .linkInfoShow, label: .none, msg: activity.ds_ext)
                return
            }
            
            // 未绑卡
            unlinkView.isHidden = false
            linkedView.isHidden = !unlinkView.isHidden
            prelinkInfoView.isHidden = true
            return
        }
        
        // 已绑卡
        let cardShow = OTTService.shared.shouldActiveShow
        unlinkView.isHidden = cardShow
        linkedView.isHidden = !unlinkView.isHidden
        prelinkInfoView.isHidden = true
        linkedView.model = card
    } }
    
    // 解绑
    @objc private func didTapUnlink() {
        delegate?.cardDetailCellDidTapUnlink()
    }
    
    // 绑卡
    @objc private func didTapLink() {
        print("-->toj: " + #function)
        guard let activity = prelinkInfoView.activity else { return }
        delegate?.cardDetailCell(self, didTapPrelinkCard: activity)
    }

    // 其他卡
    @objc private func didTapOtherCard() {
        print("-->toj: " + #function)
        prelinkInfoView.isHidden = true
        prelinkInfoView.removeFromSuperview()
        delegate?.cardDetailCellDidTapPreOtherCard(self)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupMakeLayoutSubviews()
    }
    
    private lazy var linkedView: TVCardLinkedView = {
        let view = TVCardLinkedView()
        view.unlinkButton.addTarget(self, action: #selector(didTapUnlink), for: .touchUpInside)
        return view
    }()
    
    private lazy var unlinkView: TVCardUnlinkView = {
        let view = TVCardUnlinkView()
        return view
    }()
    
    private lazy var prelinkInfoView: TVCardInfoView = {
       let view = TVCardInfoView()
        view.isHidden = true
        view.linkButton.addTarget(self, action: #selector(didTapLink), for: .touchUpInside)
        view.otherButton.addTarget(self, action: #selector(didTapOtherCard), for: .touchUpInside)
        return view
    }()
}

// MARK: - 初始化
extension TVCardDetailCell {
    
    private func setupMakeAddSubviews() {
        
        contentView.addSubview(linkedView)
        contentView.addSubview(unlinkView)
    }
    
    private func setupMakeLayoutSubviews() {
        
        linkedView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
        
        unlinkView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
    }
}
